<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar>
      <van-button class="search-btn"
                  slot="title"
                  type="info"
                  size="small"
                  round
                  @click="$router.push('/search')"
                  icon="search">搜索</van-button>
    </van-nav-bar>
    <!-- 频道列表 -->
    <van-tabs class="channel-tabs"
              animated
              swipeable
              swipe-threshold="4"
              v-model="active">
      <van-tab v-for="item in userChannels"
               :key="item.id"
               :title="item.name">
        <!-- 列表组件 -->
        <article-list :channel="item"></article-list>
      </van-tab>
      <!-- 右侧自定义内容 -->
      <!-- 占位元素 -->
      <div slot="nav-right"
           class="placeholder"></div>
      <!-- 右侧按钮 -->
      <div slot="nav-right"
           class="hamburger-btn"
           @click="isEditChannelShow=true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹框 -->
    <van-popup close-icon="close"
               position="bottom"
               close-icon-position="top-left"
               closeable
               :style="{ height: '100vh' }"
               v-model="isEditChannelShow">
      <!-- 频道编辑组件 -->
      <!-- 自定义事件函数里面的$event表示子组件传递出来的数据 -->
      <channel-edit @change-active="active=$event"
                    @close="isEditChannelShow=false"
                    :active-index="active"
                    :user-channels="userChannels"></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/channels'
import ArticleList from './components/article-list'
import ChannelEdit from './components/channel-edit'
import { getItem } from '@/utils/storage'
export default {
  components: {
    ArticleList,
    ChannelEdit
  },
  name: 'Home',
  data () {
    return {
      active: 0, // 激活项的下标
      userChannels: [], // 用户频道列表
      isEditChannelShow: false // 编辑频道弹框是否显示
    }
  },
  created () {
    this.loadUserChannels()
  },
  methods: {
    // 获取用户频道列表
    async loadUserChannels () {
      // 正确的首页逻辑
      if (this.$store.getters.token) {
        // 登录了，获取线上的
        try {
          // 发送请求
          const { data: res } = await getUserChannels()
          // 成功赋值（如果需要处理数据，在赋值之前处理）
          this.userChannels = res.data.channels
        } catch (error) {
        // 错误处理
          console.dir(error)
          this.$toast.fail('获取我的频道列表失败')
        }
      } else {
        // 未登录
        if (getItem('TOUTIAO_CHANNELS')) {
          // 本地有
          this.userChannels = getItem('TOUTIAO_CHANNELS')
        } else {
          // 本地没有，用线上默认的
          try {
          // 发送请求
            const { data: res } = await getUserChannels()
            // 成功赋值（如果需要处理数据，在赋值之前处理）
            this.userChannels = res.data.channels
          } catch (error) {
            // 错误处理
            console.dir(error)
            this.$toast.fail('获取我的频道列表失败')
          }
        }
      }
    }
  }
}
</script>
<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  // 深度选择器问题
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  /deep/ .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
  }
  /deep/ .van-tabs__line {
    width: 32px;
    height: 6px;
    background-color: #3296fa;
    bottom: 40px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>
